package com.kzn.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kzn.entity.DetailedTask;
import com.kzn.entity.Rect;
import com.kzn.service.IDetailedTaskService;
import com.kzn.util.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sorclown
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/api/detailed-task")
public class DetailedTaskController {
    private IDetailedTaskService taskService;

    public DetailedTaskController() {
    }

    @Autowired
    public DetailedTaskController(IDetailedTaskService detailedTaskService) {
        this.taskService = detailedTaskService;
    }

    /**
     * 查询已经可用但是还没有被别人接取的子任务。
     *
     * @return 还没人接取的子任务列表，格式为Json。
     */
    @RequestMapping("/selectReadyTask")
    public String selectReadyTask() {
        //设置条件
        QueryWrapper<DetailedTask> taskQueryWrapper = new QueryWrapper<>();
        taskQueryWrapper.eq("status", -1);
        taskQueryWrapper.last("limit 50");
        List<DetailedTask> list = taskService.list(taskQueryWrapper);
        //查询并返回
        return JSONUtil.parseArray(list).toString();
    }


    /**
     * 接取子任务
     *
     * @param taskId  要被接取的任务
     * @param crowdId 接取的 crowd
     * @return 是否接取成功。
     */
    @RequestMapping("/allotTaskByCrowdId")
    public boolean allotTaskByCrowdId(@RequestParam Integer taskId, @RequestParam Integer crowdId) {
        QueryWrapper<DetailedTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("taskId", taskId);
        DetailedTask task = taskService.getById(taskId);
        if (task.getCrowdId() == null) {
            if (null != taskId && null != crowdId) {
                UpdateWrapper<DetailedTask> updateWrapper = new UpdateWrapper();
                updateWrapper.eq("id", taskId);
                updateWrapper.set("status", 0);
                updateWrapper.set("crowdId", crowdId);
                return taskService.update(updateWrapper);
            }
        }
        return false;
    }

    /**
     * 根据crowdId查询子任务
     *
     * @param crowdId 发起查询的 crowdId
     * @param status  限定要查询的 status 范围
     * @return 返回根据条件查询到的子任务列表，格式为Json。
     */
    @RequestMapping("/selectTaskByCrowd")
    public String selectTaskByCrowd(@RequestParam Integer crowdId, @RequestParam(required = false) Integer status) {
        QueryWrapper<DetailedTask> taskQueryWrapper = new QueryWrapper<>();
        //如果没有 crowdId 则拒绝查询，返回null
        if (null != crowdId) {
            taskQueryWrapper.eq("crowdId", crowdId);
            //如果有status则只查询status要求的，如果没有则返回该 crowd 下所有 task。
            if (null != status) {
                taskQueryWrapper.eq("status", status);
            }else {
                taskQueryWrapper.eq("status", 0);
            }

            return JSONUtil.parseArray(taskService.list(taskQueryWrapper)).toString();
        } else {
            return null;
        }
    }


    /**
     * 上传结果
     *
     * @param rects   结果集
     * @param taskId 结果所属的子任务
     * @return
     */
    @RequestMapping(value = "/uploadResult", method = RequestMethod.POST)
    public boolean uploadResultByTask(@RequestBody List<Rect> rects , @RequestParam Integer taskId) {
        if (null != rects && null != taskId) {
            return taskService.uploadResultByTask(rects, taskId);
        } else {
            return false;
        }
    }

    /**
     * 提交子任务
     *
     * @param taskId  子任务ID
     * @param crowdId 标注人员ID
     * @return 是否提交成功
     */
    @RequestMapping(value = "/submit", method = RequestMethod.GET)
    public boolean submitTask(@RequestParam Integer taskId, @RequestParam Integer crowdId) {
        if (null != taskId && null != crowdId) {
            return taskService.submit(taskId, crowdId);
        } else {
            return false;
        }
    }

    @RequestMapping(value = "/getPictureByTaskId", method = RequestMethod.GET)
    public ResponseVo<String> getPictureByTaskId(@RequestParam Integer taskId) {
        if (null != taskId) {
            return taskService.getPictureByTaskId(taskId);
        }
        return null;
    }




}
